﻿namespace VisualSVN.Core.Licensing
{
    using System;
    using System.IO;
    using System.Text;
    using VisualSVN.Core;

    public class OldLicenseSerializer
    {
        private const int EvaluationPeriod = 30;

        public License Deserialize(string key, IDecoder decoder)
        {
            byte[] buffer = Base32Decoder.Decode(key);
            if (IsEmpty(buffer))
            {
                return null;
            }
            byte[] buffer2 = decoder.Decode(buffer);
            if (IsEmpty(buffer2))
            {
                return null;
            }
            using (MemoryStream stream = new MemoryStream(buffer2))
            {
                return ReadLicenseFromStream(stream);
            }
        }

        private static bool IsEmpty(Array value)
        {
            if (value != null)
            {
                return (value.Length == 0);
            }
            return true;
        }

        private static string ReadBytePrefixedString(BinaryReader reader)
        {
            int count = reader.ReadByte();
            char[] chArray = reader.ReadChars(count);
            if (chArray.Length != count)
            {
                throw new EndOfStreamException();
            }
            return new string(chArray);
        }

        private static DateTime ReadDateTime(BinaryReader reader)
        {
            return new DateTime(reader.ReadInt64());
        }

        private static License ReadLicenseFromStream(Stream stream)
        {
            License license2;
            try
            {
                License license = new License();
                BinaryReader reader = new BinaryReader(stream, Encoding.Unicode);
                license.Type = (LicenseType) reader.ReadByte();
                license.PurchaseId = "shareit:" + reader.ReadInt64().ToString();
                license.Version = reader.ReadByte();
                DateTime time = ReadDateTime(reader);
                string str = ReadBytePrefixedString(reader);
                string str2 = ReadBytePrefixedString(reader);
                license.LicensedTo = str;
                if ((str2.Length > 0) && (str2 != str))
                {
                    license.LicensedTo = license.LicensedTo + ", " + str2;
                }
                license.Capacity = 1;
                license.Binding = LicenseBinding.User;
                if (license.Type == LicenseType.Personal)
                {
                    license.StartTime = DateTime.MinValue;
                    license.EndTime = DateTime.MaxValue;
                    license.PurchaseDate = time;
                }
                else
                {
                    license.StartTime = time;
                    license.EndTime = time.AddDays(30.0);
                    license.PurchaseDate = time;
                }
                license2 = license;
            }
            catch (Exception exception)
            {
                throw new LicensingException("Can not parse license.", exception);
            }
            return license2;
        }

        public string Serialize(License license, IEncoder encoder)
        {
            byte[] buffer;
            using (MemoryStream stream = new MemoryStream())
            {
                WriteLicenseToStream(license, stream);
                buffer = stream.ToArray();
            }
            return Base32Encoder.Encode(encoder.Encode(buffer));
        }

        private static void WriteBytePrefixedString(BinaryWriter writer, string str)
        {
            writer.Write((byte) str.Length);
            writer.Write(str.ToCharArray());
        }

        private static void WriteDateTime(BinaryWriter writer, DateTime date)
        {
            writer.Write(date.Ticks);
        }

        private static void WriteLicenseToStream(License license, Stream stream)
        {
            string licensedTo = license.LicensedTo;
            if (licensedTo == null)
            {
                licensedTo = "";
            }
            string str = "";
            BinaryWriter writer = new BinaryWriter(stream, Encoding.Unicode);
            writer.Write((byte) license.Type);
            writer.Write((long) 0L);
            writer.Write(license.Version);
            if (license.Type == LicenseType.Personal)
            {
                WriteDateTime(writer, license.PurchaseDate);
            }
            else
            {
                WriteDateTime(writer, license.StartTime);
            }
            WriteBytePrefixedString(writer, licensedTo);
            WriteBytePrefixedString(writer, str);
        }
    }
}

